package com.ereport.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.ereport.entity.Executives;
import com.ereport.util.ApplicationContext;

public class ExecutiveDAO {

static Transaction tx;
	
	private  void endOperations(Session session) {
		
		session.close();
		session=null;
	}

	private  Session startOperations()throws Exception {
		
		Session session = ApplicationContext.getHiberSession().openSession();
		tx=session.beginTransaction();
		return session;
		
	}
	public Executives validatCredentials(String userid, String pass) throws Exception {
		Session session = startOperations();
		Query query = session.createQuery("from Executives where exe_email=:email and pwd=:pass");
		query.setString("email", userid);
		query.setString("pass", pass);
		Executives u = (Executives) query.uniqueResult();
		endOperations(session);
		return u;
	}

	public List<Executives> getExecutives() throws Exception {
		Session session = startOperations();
		Query query = session.createQuery("from Executives where is_admin=false");
		List<Executives> elist=query.list();
		endOperations(session);
		return elist;
	}

	public Executives getById(int exeid) throws Exception {
		Session session = startOperations();
		Query query = session.createQuery("from Executives where id=:id");
		query.setInteger("id", exeid);
		Executives u = (Executives) query.uniqueResult();
		endOperations(session);
		return u;
	}

	public int insert(Executives exe) throws Exception {
		Session session = startOperations();
		session.saveOrUpdate(exe);
		tx.commit();
		endOperations(session);
		return 0;
		
	}

	public Executives emailValid(String email) throws Exception {
		Session session = startOperations();
		Query query = session.createQuery("from Executives where exe_email=:email");
		query.setString("email", email);
		Executives u = (Executives) query.uniqueResult();
		endOperations(session);
		return u;
	}

	public Executives getExecutive(String exeName) throws Exception {
		Session session = startOperations();
		Query query = session.createQuery("from Executives where exe_name=:name");
		query.setString("name", exeName);
		Executives u = (Executives) query.uniqueResult();
		endOperations(session);
		return u;
	}

	public Executives getPassword(String email) throws Exception {
		Session session = startOperations();
		Query query = session.createQuery("from Executives where exe_email=:email");
		query.setString("email", email);
		Executives u = (Executives) query.uniqueResult();
		endOperations(session);
		return u;
	}

	
}
